package com.xjcs.saplingplan.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.xjcs.saplingplan.room.bean.AppUserInfo
import com.xjcs.saplingplan.room.bean.countdown.CountdownBean
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.room.bean.focus.FocusBean
import com.xjcs.saplingplan.room.dao.AppUserDao
import com.xjcs.saplingplan.room.dao.PlanCateDao
import com.xjcs.saplingplan.room.dao.CountdownDao
import com.xjcs.saplingplan.room.dao.FocusDao

//数据库名称
const val DATABASE_NAME = "sapling_plan"

@Database(
    version = 1,
    entities = [AppUserInfo::class, FocusBean::class, CountdownBean::class, PlanCateBean::class],
    exportSchema = true
)
abstract class AppDatabase : RoomDatabase() {

    abstract fun appUserDao(): AppUserDao

    abstract fun focusDao(): FocusDao

    abstract fun countdownDao(): CountdownDao

    abstract fun planCateDao(): PlanCateDao

    companion object {

        @Volatile
        private var instance: AppDatabase? = null

        /**
         * 获取数据库实例（单例）
         */
        fun getInstance(context: Context): AppDatabase {
            if (instance == null) {
                synchronized(AppDatabase::class) {
                    if (instance == null) {
                        instance = Room.databaseBuilder(
                            context, AppDatabase::class.java,
                            DATABASE_NAME
                        )
                            .allowMainThreadQueries()
                            .build()
                    }
                }
            }
            return instance!!
        }
    }

}